package com.iasku.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.iasku.constant.Constant;
import com.iasku.iaskueleclass_phone.R;
import com.iasku.manager.IaskuManager;
import com.iasku.manager.ViewManager;

public class StatisticsFragment extends BaseFragment {
    private WebView mWorkStatisticsWv;
    
    @SuppressLint("SetJavaScriptEnabled")
    private void initView(View view){
        mBarTitleSubjectTv.setVisibility(View.GONE);
        mWorkStatisticsWv = (WebView) view.findViewById(R.id.wv_work_statistics);
        mWorkStatisticsWv.getSettings().setJavaScriptEnabled(true);
        mWorkStatisticsWv.getSettings().setSupportZoom(true);
        mWorkStatisticsWv.getSettings().setBuiltInZoomControls(true);
        mWorkStatisticsWv.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                mProgressDialog.dismiss();
                mWorkStatisticsWv.setVisibility(View.VISIBLE);
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        ViewManager.getInstance().setWebview(mWorkStatisticsWv);
        mProgressDialog.show();
        loadUrl();
        
        mBarSearchBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mProgressDialog.show();
                loadUrl();
            }
        });
    }
    
    private void loadUrl(){
        initTime();
        String url =  Constant.SERVERURL + "/pad_make_paper/paper_lists2?username=" + Constant.USER_IASKUNAME
                + "&paper_date_start=" + time_start + "&paper_date_end=" + time_end + "&sid=" +
                IaskuManager.getInstance().getSubjectId() + "&_type=auto";
        mWorkStatisticsWv.setVisibility(View.INVISIBLE);
        mWorkStatisticsWv.loadUrl(url);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.statistics_fragment_layout, null);
        initTitleBar(R.string.statistics_title);
        initView(mRootView);
        return mRootView;
    }
}
